package com.hhb.gulimall.product.controller;

import com.hhb.common.utils.PageUtils;
import com.hhb.common.utils.R;
import com.hhb.common.valid.AddGroup;
import com.hhb.gulimall.product.entity.BrandEntity;
import com.hhb.gulimall.product.service.BrandService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 品牌
 *
 * @author hcfs
 * @email 599981113@qq.com
 * @date 2021-04-28 11:32:44
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation(value = "上传品牌")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*/){
/*
        *//**
         * 判断是否有错误
         *//*
        if (result.hasErrors()){
            Map<String,String> map = new HashMap<>();

            result.getFieldErrors().forEach((fieldError)->{
                *//**
                 * 错误的提示信息和字段名
                 * @message 错误的提示信息
                 * @field   字段名
                 *//*
                String message = fieldError.getDefaultMessage();
                String field = fieldError.getField();
                map.put(field,message);
            });
//            throw new RuntimeException("错了错了");
            return R.error(400,"提交的数据不合法").put("data",map);
        }else {
            *//**
             * 如果没错则执行业务
             *//*

        }*/
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){


		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
